package com.example.login.fragment;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.login.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;

public class HomeActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private TabLayout bottomNav;

    private HomeVpAdapter mVpAdapter;
    private BottomNavigationView mBottomNavigationView;
    private Menu mMenuItem;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mViewPager = findViewById(R.id.vp_content);
        bottomNav = findViewById(R.id.tl_bottom_nav);

        mVpAdapter = new HomeVpAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mVpAdapter);

        bottomNav.setupWithViewPager(mViewPager);

        mBottomNavigationView = findViewById(R.id.bnv_bottom_nav);
        mMenuItem = mBottomNavigationView.getMenu();


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mBottomNavigationView.getMenu().getItem(position).setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mBottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.nav_one:
                    mViewPager.setCurrentItem(0);
                    break;
                case R.id.nav_two:
                    mViewPager.setCurrentItem(1);
                    break;
                case R.id.nav_three:
                    mViewPager.setCurrentItem(2);
                    break;
                case R.id.nav_four:
                    mViewPager.setCurrentItem(3);
                    break;
            }
            return false;
        });

    }
}
